#include "tool_qrdecoder.h"
#include <QThread>

Tool_qrdecoder::Tool_qrdecoder(QObject *parent) : QObject(parent)
{
#ifndef QT_NO_DEBUG
        qDebug()<<"Tool_qrdecoder::Tool_qrdecoder";
#endif
        connect(this, &Tool_qrdecoder::startDecoder, this, &Tool_qrdecoder::onstartDecoder);



}

Tool_qrdecoder::~Tool_qrdecoder()
{
    if(m_timerId >= 0)
        killTimer(m_timerId);
}

void Tool_qrdecoder::dectanddecoderQRcode(QImage img)
{
    QImage image;
    {
        QMutexLocker locker(&m_mutex);
        if(m_image.isNull())
            return;
        image = m_image;
        m_image = QImage();
    }
    cv::QRCodeDetector  decoder;
    cv::Mat  mat,bbox,rect;
    mat=QImage2cvMat(image);
    if(decoder.detect(mat,bbox)){
#ifndef QT_NO_DEBUG
        qDebug()<<"Tool_qrdecoder::dectanddecoderQRcode getQRCode";
#endif
         std::string result = decoder.decode(mat,bbox);
         QString res=QString::fromStdString(result);

         if(res.indexOf(QLatin1String("device/info/credic?"))!=-1){
             emit qrDecodersuccess(res);
         }
         else {
             emit qrDecoderfailed();

         }

#ifndef QT_NO_DEBUG
        qDebug() << "Tool_qrdecoder::dectanddecoderQRcoder get res"<<res;
#endif
    }
}

/*!
 * \brief Tool_qrdecoder::setImage
 * \param img
 */
void Tool_qrdecoder::setImage(QImage img)
{
#ifndef QT_NO_DEBUG
        qDebug() << "Tool_qrdecoder::setImage";
#endif
    {
        QMutexLocker locker(&m_mutex);
            m_image=img;
    }
}
/*!
 * \brief 开启计时器
 */
void Tool_qrdecoder::onstartDecoder()
{
    if(m_timerId<0){
        m_timerId=startTimer(200);
#ifndef QT_NO_QDEBUG
        qDebug()<<"Tool_qrdecoder::startDecoder success";
#endif
    }else{
#ifndef QT_NO_QDEBUG
        qDebug()<<"Tool_qrdecoder::startDecoder  waring";
#endif
    }

}
/*!
 * \brief 停止计时器
 */
void Tool_qrdecoder::stopDecoder()
{
    if(m_timerId >= 0){
            killTimer(m_timerId);
#ifndef QT_NO_DEBUG
        qDebug()<<"Tool_qrdecoder::stopDecode";
#endif
    }
}
/*!
 * \brief 计时器事件抓取,处理图片
 * \param event
 */
void Tool_qrdecoder::timerEvent(QTimerEvent *event)
{
#ifndef QT_NO_DEBUG
        qDebug()<<"Tool_qrdecoder::timerEvent";
#endif
    if(event->timerId() == m_timerId )
    {
        dectanddecoderQRcode(m_image);
        return;
    }
    QObject::timerEvent(event);
}
/*!
 * \brief QImage转换为cv：：Mat
 * \param img  需要转换的图像
 * \return   等效img的cv：：Mat数据
 */
cv::Mat Tool_qrdecoder::QImage2cvMat(const QImage img)
{
    cv::Mat mat;
    QImage image=img;
#ifndef QT_NO_DEBUG
        qDebug()<<"Tool_qrdecoder::QImage2cvMat"<<image.format();
#endif
    switch(image.format())
    {
    case QImage::Format_ARGB32:
    case QImage::Format_RGB32:
    case QImage::Format_ARGB32_Premultiplied:
        mat = cv::Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
        break;
    case QImage::Format_RGB888:
        mat = cv::Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
        cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB);
        break;
    case QImage::Format_Indexed8:
        mat = cv::Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
        break;
    }
    return mat;

}

Tool_qr::Tool_qr()
{
    m_tool_qrdecoder=new Tool_qrdecoder;

    QThread *thread=new QThread;

    connect(m_tool_qrdecoder, &Tool_qrdecoder::destroyed, thread, &QThread::quit);
    connect(thread, &QThread::finished, thread, &QThread::deleteLater);

    m_tool_qrdecoder->moveToThread(thread);
    thread->start();

    m_tool_qrdecoder->startDecoder();
}

Tool_qr::~Tool_qr()
{
    m_tool_qrdecoder->deleteLater();
}

void Tool_qr::setIame(const QImage img)
{
    m_tool_qrdecoder->setImage(img);
}
